package com.richard.library.basic.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import androidx.recyclerview.widget.RecyclerView;

import com.richard.library.context.AppContext;

/**
 * <pre>
 * Description : View 工具类
 * Author : admin-richard
 * Date : 2021-07-30 17:31
 * Changelog:
 * Version            Date            Author              Detail
 * ----------------------------------------------------------------------
 * 1.0         2021-07-30 17:31     admin-richard         new file.
 * </pre>
 */
public final class ViewUtil {

    private ViewUtil() {
    }

    /**
     * 判断RecyclerView是否滑动到顶部
     */
    public static boolean isToTop(RecyclerView recyclerView) {
        return !recyclerView.canScrollVertically(-1);
    }

    /**
     * 判断RecyclerView是否滑动到底部
     */
    public static boolean isToBottom(RecyclerView recyclerView) {
        return !recyclerView.canScrollVertically(1);
    }

    /**
     * 触摸点是否在视图范围内
     *
     * @param view 视图
     * @param ev   触摸点
     * @return 是否在视图范围内
     */
    public static boolean inRangeOfView(View view, MotionEvent ev) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];
        return !(ev.getX() < x) && !(ev.getX() > (x + view.getWidth()))
                && !(ev.getY() < y) && !(ev.getY() > (y + view.getHeight()));
    }

    /**
     * 获取该view的在屏幕的x和y坐标
     *
     * @param view view
     * @return locations[0]:x坐标、locations[1]:y坐标
     */
    public static int[] getLocOnScreen(View view) {
        int[] locations = new int[2];
        view.getLocationOnScreen(locations);
        return locations;
    }

    /**
     * 获取该view的在window的x和y坐标
     *
     * @param view view
     * @return locations[0]:x坐标、locations[1]:y坐标
     */
    public static int[] getLocOnWindow(View view) {
        int[] locations = new int[2];
        view.getLocationInWindow(locations);
        return locations;
    }

    /**
     * 获得屏幕高度
     */
    public static int getScreenWidth() {
        WindowManager wm = (WindowManager) AppContext.get().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 获得屏幕宽度
     */
    public static int getScreenHeight() {
        WindowManager wm = (WindowManager) AppContext.get()
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    /**
     * 获得副屏幕高度
     */
    public static int getDisplayWidth(Display display) {
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 获得副屏幕宽度
     */
    public static int getDisplayHeight(Display display) {
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    /**
     * 获取状态栏高度
     *
     * @return 状态栏高度
     */
    public static int getStatusHeight() {
        // 获得状态栏高度
        int resourceId = AppContext.getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        return AppContext.getResources().getDimensionPixelSize(resourceId);
    }

    /**
     * 获取当前屏幕截图，包含状态栏
     */
    public static Bitmap snapShotWithStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        int width = getScreenWidth();
        int height = getScreenHeight();
        Bitmap bp = null;
        bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
        view.destroyDrawingCache();
        return bp;

    }

    /**
     * 获取当前屏幕截图，不包含状态栏
     */
    public static Bitmap snapShotWithoutStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;

        int width = getScreenWidth();
        int height = getScreenHeight();
        Bitmap bp = null;
        bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
                - statusBarHeight);
        view.destroyDrawingCache();
        return bp;

    }
}
